package com.lgt_enterprise_android.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.utils.CommonUtil;

import java.util.ArrayList;

public class MyGridViewAdapter extends BaseAdapter {
    //上下文对象
    private Context context;

    private ArrayList<String> lists;

    private int MAX_IMAGE_COUNT = 6;

//    IDeleteClickListener deleteClickListener;
//
//
//    public interface IDeleteClickListener {
//        public void onDelete(int pos);
//    }


//    public void setList(ArrayList<String> lists) {
//        this.lists = lists;
//    }

    public ArrayList<String> getList() {
        return lists;
    }

    public MyGridViewAdapter(Context context, ArrayList<String> lists) {
        this.context = context;
        this.lists = lists;
    }


    public void addPic(String pic) {
        lists.add(pic);
    }


    @Override
    public int getCount() {
        if (lists.size() < MAX_IMAGE_COUNT) {
            return lists.size() + 1;
        }
        return MAX_IMAGE_COUNT;
    }

    @Override
    public String getItem(int item) {
        return lists.get(item);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    //创建View方法
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_image_view, parent, false);
            holder = new ViewHolder();

            holder.imageView = (ImageView) convertView.findViewById(R.id.imgView);
            holder.imgDelete = (ImageView) convertView.findViewById(R.id.imgDelete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == lists.size()) {
            holder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_addimage));
            holder.imgDelete.setVisibility(View.GONE);
        } else {
            String imgPath = lists.get(position);
            if (imgPath.startsWith("http")) {
                Glide.with(context).applyDefaultRequestOptions(CommonUtil.getGlideOptions()).load(imgPath).into(holder.imageView);
            } else {
                BitmapFactory.Options newOpts = new BitmapFactory.Options();
                newOpts.inSampleSize = 8;
                Bitmap bm = BitmapFactory.decodeFile(imgPath, newOpts);
                holder.imageView.setImageBitmap(bm);
            }

            holder.imgDelete.setVisibility(View.VISIBLE);
            holder.imgDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    lists.remove(position);
                    notifyDataSetChanged();
                }
            });
        }
        return convertView;
    }

    static class ViewHolder {
        ImageView imageView;
        ImageView imgDelete;
    }
}

